package com.yachang.qym.usbhost.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.model.ContactModel;
import com.yachang.qym.usbhost.utils.AllData;
import com.yachang.qym.usbhost.utils.SendData;

import java.util.ArrayList;
import java.util.List;

public class GroupManagerActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private TextView mTvTemporaryDeploy;
    private TextView mTvExitGroup;
    private LinearLayout mLayoutOutGroup;
    private LinearLayout mLayoutInGroup;
    private RelativeLayout mLayoutEnter,mLayoutExit;
    private List<ContactModel> mContactModel = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_manager);
        initView();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private void initView(){
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("对讲分组");
        mTvTemporaryDeploy = (TextView) findViewById(R.id.tv_temporary_deploy);
        mTvExitGroup = (TextView) findViewById(R.id.tv_exit_group);
        mLayoutInGroup = (LinearLayout) findViewById(R.id.layout_in_group);
        mLayoutOutGroup = (LinearLayout) findViewById(R.id.layout_out_group);
        mLayoutEnter = (RelativeLayout) findViewById(R.id.layout_enter);
        mLayoutExit = (RelativeLayout) findViewById(R.id.layout_exit);
        mTvExitGroup.setClickable(false);
        mTvTemporaryDeploy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvExitGroup.setClickable(true);
                mTvTemporaryDeploy.setClickable(false);
                mLayoutEnter.setBackgroundColor(getResources().getColor(R.color.gray));
                mLayoutExit.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                int length = 0;
                for(int i = 0; i < mContactModel.size(); i++){
                    if(!mContactModel.get(i).getIsWhere()){
                        length ++;
                    }
                }
                SendData.data = SendData.sendEnterDeploy(mContactModel, length);
                SendData.needSend = true;
            }
        });
        mTvExitGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvExitGroup.setClickable(false);
                mTvTemporaryDeploy.setClickable(true);

                mLayoutEnter.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                mLayoutExit.setBackgroundColor(getResources().getColor(R.color.gray));

                SendData.data = SendData.sendExitDeploy();
                SendData.needSend = true;
            }
        });
        mContactModel.clear();
        for(int i = 0; i < AllData.contactModels.size(); i++){
            if(AllData.contactModels.get(i).getStyle() == 0){
                mContactModel.add(AllData.contactModels.get(i));
            }
        }
        showGroupInfo();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home :
                GroupManagerActivity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showGroupInfo(){
        mLayoutOutGroup.removeAllViews();
        mLayoutInGroup.removeAllViews();
        for(int i = 0; i < mContactModel.size(); i ++){
            if(mContactModel.get(i).getIsWhere()) {
                mLayoutOutGroup.addView(addOutGroupView(i));
            } else {
                mLayoutInGroup.addView(addInGroupView(i));
            }
        }
    }

    private View addOutGroupView(final int count){
        final View item = LayoutInflater.from(this).inflate(R.layout.item_group_out, null, false);
        TextView tvContactName = (TextView) item.findViewById(R.id.tv_contact_name);
        TextView tvContactStyle = (TextView) item.findViewById(R.id.tv_contact_style);
        tvContactStyle.setText("组呼");
        tvContactName.setText(mContactModel.get(count).getName());
        LinearLayout layout = (LinearLayout) item.findViewById(R.id.layout_do_in_group);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContactModel.get(count).setIsWhere(false);
                showGroupInfo();
            }
        });
        return item;
    }

    private View addInGroupView(final int count){
        final View item = LayoutInflater.from(this).inflate(R.layout.item_group_in, null, false);
        TextView tvContactName = (TextView) item.findViewById(R.id.tv_contact_name);
        TextView tvContactStyle = (TextView) item.findViewById(R.id.tv_contact_style);
        tvContactStyle.setText("组呼");
        tvContactName.setText(mContactModel.get(count).getName());
        RelativeLayout layout = (RelativeLayout) item.findViewById(R.id.layout_do_out_group);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContactModel.get(count).setIsWhere(true);
                showGroupInfo();
            }
        });
        return item;
    }

}
